﻿using NoteSystem.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace NoteSystem
{
    public partial class Register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitCity();
                ddlProvince.SelectedValue="1";
            }
            
        }
        public void InitCity()
        {
            DataSet ds = DbHelper.Query("select * from S_Province");
            ddlProvince.DataSource = ds;
            ddlProvince.DataTextField = "ProvinceName";
            ddlProvince.DataValueField = "ProvinceID";
            ddlProvince.DataBind();

           
        }
        protected void Button2_Click(object sender, EventArgs e)
        {
            string account=txtAccount.Text.Trim();
            string pwd=txtPwd.Text.Trim();
            DateTime birthday=DateTime.Parse(txtBirthday.Text.Trim());
            string imgUrl = Image1.ImageUrl;
            int city = Convert.ToInt32(ddlCity.SelectedValue);//获取下拉框的值
            int gender =Convert.ToInt32( rblGender.SelectedValue);
            string phone=txtPhone.Text.Trim();
            string email=txtEmail.Text.Trim();
            string qq=txtQQ.Text.Trim();

            string sql = string.Format(@"insert into users 
             values('{0}','{1}','{2}',{3},{4},'{5}','{6}','{7}','{8}',0,0,'{9}',0)",account,pwd, birthday.ToString("yyyy-MM-dd"), city,gender,imgUrl,phone,email,qq,DateTime.Now);
            int flag = DbHelper.ExecuteSql(sql);
            if (flag>0)
            {
                Response.Write("<script>alert('注册成功')</script>");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string exList = ".jpg.gif.png";
            //1.捕获文件名称
            string imgName = fulImg.FileName;
            string exName = imgName.Substring(imgName.LastIndexOf('.'));//截取扩展名
            if (!exList.Contains(exName))
            {
                Response.Write("<script>alert('不支持该图片格式')</script>");
                return;
            }
            if (fulImg.FileContent.Length>2*1024*1024)
            {
                Response.Write("<script>alert('图片大小不能超过1M')</script>");
                return;
            }
            //2.确定文件保存的物理地址
            string path = Server.MapPath("images")+"\\"+imgName;
            //3.进行文件保存
            fulImg.SaveAs(path);
            Image1.ImageUrl = "/images/"+imgName;

        }

        protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pid = Convert.ToInt32(ddlProvince.SelectedValue);
            DataSet ds = DbHelper.Query("select * from S_City where ProvinceID="+ pid);
            ddlCity.DataSource = ds;
            ddlCity.DataTextField = "CityName";
            ddlCity.DataValueField = "CityID";
            ddlCity.DataBind();

        }
    }
}